package com.itheima.edu.info.manager.controller;

import com.itheima.edu.info.manager.domain.Student;
import com.itheima.edu.info.manager.service.StudentService;

import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;
import java.util.Scanner;

public abstract class BaseStudentController {
    //创建业务员对象
    private StudentService service = new StudentService();
    private Scanner sc = new Scanner(System.in);

    public final void start() {
        login();
        while (true) {
            System.out.println("--------欢迎来到 <学生> 管理系统--------");
            System.out.println("请输入您的选择: 1.添加学生  2.删除学生  3.修改学生  4.查看学生  5.退出");
            String choice = sc.next();
            switch (choice) {
                case "1":
                    addStudent();
                    break;
                case "2":
                    deleteStudentById();
                    break;
                case "3":
                    updateStudent();
                    break;
                case "4":
                    findAllStudent();
                    break;
                case "5":
                    System.out.println("感谢您使用学生管理系统,再见!");
                    return;
                default:
                    System.out.println("您的输入有误,请重新输入");
                    break;
            }
        }
    }
    public static void login() {
        try {
            Properties pop = new Properties();
            pop.load(new FileReader("day13_字符流&properties\\b.txt"));

            Scanner sc = new Scanner(System.in);
            System.out.println("请输入用户名:");
            String userName = sc.next();
            System.out.println("请输入密码:");
            String passWord = sc.next();

            String realPass = pop.getProperty(userName);
            if(passWord.equals(realPass)){
                System.out.println("登录成功");
            }else{
                System.out.println("登录失败");
            }
        } catch (IOException e) {

        }
    }

    public final void updateStudent() {
        System.out.println("请输入要修改的学生学号:");
        String updateId = inputStudentId(true);
        Student newStu = inputStudentInfo(updateId);
        service.updateStudent(updateId, newStu);
        System.out.println("修改成功!");
    }

    public final void deleteStudentById() {
        System.out.println("请输入要删除的学生学号:");
        String delId = inputStudentId(true);
        service.deleteStudentById(delId);
        System.out.println("删除成功");
    }
    //客服类的查看学生功能
    public final void findAllStudent() {
        //指挥业务员把系统所用数据拿过来
        Student[] stus = service.findAllStudent();
        //根据业务员返回的结果来给出对应的用户信息提示
        if (stus == null) {
            System.out.println("查无信息,请添加后重试");
            return;
        }
        System.out.println("学号\t\t姓名\t年龄\t生日");
        for (int i = 0; i < stus.length; i++) {
            Student stu = stus[i];
            if (stu != null) {
                System.out.println(stu.getId() + "\t" + stu.getName() + "\t" + stu.getAge() + "\t\t" + stu.getBirthday());
            }
        }
    }
    //客服类的添加学生功能
    public final void addStudent() {
        System.out.println("请输入学生学号:");
        String id = inputStudentId(false);
        Student stu = inputStudentInfo(id);
        boolean res = service.addStudent(stu);
        if (res) {
            System.out.println("添加成功");
        } else {
            System.out.println("添加失败");
        }
    }
    //键盘录入一个可用id并返回
    public final String inputStudentId(boolean exit) {//    !!!此处的业务逻辑有待加深理解!!!
        String id;
        while (true) {
            id = sc.next();
            boolean exists = service.isExists(id);
            if (exists == exit) {
                break;
            } else {
                System.out.println("您输入的学号不可用,请重新输入:");
            }
        }
        return id;
    }

    public abstract Student inputStudentInfo(String id);
}
